const Actions = require('./actions');
const DB = require('app/db.js');
import { Storage } from 'clientConfig/util/StoreData'
import { Toast } from 'saltui';
import { Control } from 'react-keeper';
import { ready } from 'clientConfig/util/queryurlfield'
const device = require('clientConfig/util/jsapi/device');
module.exports = Reflux.createStore({
    listenables: [Actions],
    data: {

    },
    onFetch: function (params, cb) {
        let t = this;
        if (!params.id) { return; }
        DB.Teacher.get_repair_detail({
            schoolId: Storage.get('user.schoolId'),
            teacherId: Storage.get('user.teacherId'),
            id: params.id
        })
            .then((content) => {
                if (content && content.code == 1) {
                    if (content && content.data) {
                        content = content.data;
                        for (let [key, value] of Object.entries(content)) {
                            t.data[key] = value;
                        }
                        t.updateComponent();
                    }
                } else {
                    Toast.show({
                        type: 'error',
                        content: '系统异常'
                    });
                }
            })
            .catch(function (error) {
                Toast.show({
                    type: 'error',
                    content: '系统异常'
                });
            });
    },
    onGetRepairStatus: function (cb) {
        let t = this;
        DB.Teacher.get_repair_status({
            schoolId: Storage.get('user.schoolId'),
        })
            .then((content) => {
                if (content && content.code == 1) {
                    if (content && content.data) {
                        t.data.repairStatus = content.data.dataList;
                         t.updateComponent();
                         cb && cb();
                    }
                } else {
                    Toast.show({
                        type: 'error',
                        content: '系统异常'
                    });
                }
            })
            .catch(function (error) {
                Toast.show({
                    type: 'error',
                    content: '系统异常'
                });
            });
    },
    onSaveRepairFeedback: function (params, cb) {
        let t = this;
        DB.Teacher.save_repair_feedback.config.query = {
            schoolId: Storage.get('user.schoolId'),
            teacherId: Storage.get('user.teacherId'),
            remark: params.remark,
            repairStatus: params.repairStatus,
            id: params.id
        }
        DB.Teacher.save_repair_feedback()
            .then((content) => {
                if (content && content.code == 1) {
                    Toast.show({
                        type: 'success',
                        content: '反馈成功'
                    });
                    Control.go(-1)
                } else {
                    Toast.show({
                        type: 'error',
                        content: '系统异常'
                    });
                }
            })
            .catch(function (error) {
                Toast.show({
                    type: 'error',
                    content: '系统异常'
                });
            });
    },
    onGetPersonList: function (cb) {
        let t = this;
        DB.Teacher.get_person_list({
            schoolId: Storage.get('user.schoolId'),
        })
            .then((content) => {
                if (content && content.code == 1) {
                    t.data.personList = content.data.list;
                    t.updateComponent();
                    cb && cb()
                } else {
                    Toast.show({
                        type: 'error',
                        content: '系统异常'
                    });
                }
            })
            .catch(function (error) {
                
                Toast.show({
                    type: 'error',
                    content: '系统异常'
                });
            });
    },
    onAssignPersonTask: function (params, cb) {
        let t = this;
        let { id, assignTaskPerson, assignTaskPersonPhone, assignTaskPersonId } = params;
        ready(() => { device.showPreloader() })
        DB.Teacher.assign_person_task.config.query = {
            schoolId: Storage.get('user.schoolId'),
            teacherId: Storage.get('user.teacherId'),
            id,
            assignTaskPerson,
            assignTaskPersonPhone,
            assignTaskPersonId
        } 
        DB.Teacher.assign_person_task()
            .then((content) => {
                ready(() => { device.hidePreloader() })
                if (content && content.code == 1) {                    
                    Control.go(-1)
                } else {
                    Toast.show({
                        type: 'error',
                        content: '系统异常'
                    });
                }
            })
            .catch(function (error) {
                Toast.show({
                    type: 'error',
                    content: '系统异常'
                });
            });
    },
    updateComponent: function () {
        this.trigger(this.data);
    }
});
